--[[
######################################################################################################
## O curse so aparece se houver uma cruz de lenha comum com 5 de altura e na cabeca um bloco de osso.
######################################################################################################
--]]

if minetest.setting_getbool("lunomobs_curseflame")~=false then
	minetest.setting_set("lunomobs_curseflame","true")
end

lunomobs:register_mob("lunomobs:curse", {
	mob_name="MALDITO HARRY CURSE",
	type = "monster",
	hp_max = 30, --Padrao = 4
	armor = 75,--padrao 200
	xp_give = 70,
	--pos ={0.0,0.0,0.0},
	jump = true, 
	visual = "mesh",
	mesh="mobs_humanoid.b3d",
	textures = {"mobs_humanoid_death1.png"},
	collisionbox={-0.3,-1.4,-0.3, 0.3,0.8,0.3}, visual_size={x=1,y=1},
	--collisionbox={-0.5,-1.9,-0.8, 0.8,1.3,0.8}, visual_size={x=2,y=2},
	makes_footstep_sound = false, --Ele flutua por isso nao faz barrulo na grama.
	disable_fall_damage=true, --Nao leva dano quando cai do alto.
	view_range = 10,
	walk_velocity = 1.5,
	run_velocity = 3,
	damage = 5, --Da um dano arretado. (4x = you death)
	drops = { 
		{name="lunomobs:curse", chance = 100, min=1, max=1,}, --Probabilidade e de 1:100 (1%) de cair entre 1 a 3 "minemoedas"
		{name="lunorecipes:magicdust", chance=2, min=1, max=1,}, --Probabilidade é de 1:2 (=50%) de cair entre 1 "po encantado"
		--{name="default:gold_lump", chance = 20, min=1, max=3 ,},	--Probabilidade e de 1:20 (5%) de cair entre 1 a 3 "punhados de ouro"
	},
	drawtype = "front",
	water_damage = 0.05, -- 1=Para ele nao ficar lutando de dia debaixo da agua
	lava_damage = 10,
	light_damage = 1, --1=Morte lenta
	attack_type = "dogfight",
	animation = {
		speed_normal=15, speed_run=30,
		stand_start=0, stand_end=79,
		walk_start=0, walk_end=79, --Para parecer q esta flutuando. (Nao mexe as pernas)
		run_start=189, run_end=199, --Para parecer q esta atacando parado em quando se locomove.
		punch_start=189, punch_end=199, --Ataca Parado.
	},
	sounds = {
		random = "mobs_skull_cartoon",
		attack = "sword_1",
		hit = "sword_2",
		--die = "mobs_death_die",
		die = "mobs_death_born",
	},
})

--lunomobs:register_spawn(name, nodes, max_light, min_light, chance, active_object_count, max_height, spawn_func)
lunomobs:register_spawn('lunomobs:curse', {"bones:bones"}, 4, 0, 1, 1, 31000, function(pos, node) --Executa esta funcao ao inves de desovar o mob.
	if pos~=nil and node~=nil then
		
		if pos.y < -5 then return end
		
		local hora = minetest.get_timeofday() * 24 --Converte hora de 0~1 para formato 0h~24h
		if hora>18.4 or hora<5.5 then --SO QUANDO ESTA ESCURO OU ESCURECENDO
			local RaioDeCurse = 500
			local myTarget = nil
			local deathCurse = nil
	

			if minetest.setting_getbool("display_mob_spawn") then
				minetest.chat_send_all(node.name.."[".. pos.x ..",".. pos.y ..",".. pos.z .."]")
			end
			
			for _,obj in pairs(minetest.env:get_objects_inside_radius(pos, RaioDeCurse)) do --Testa se ja ha algum Curse por perto
				--local objname = self.name
				--local objname = obj:get_luaentity().name
				if obj~=nil and obj:is_player() then
					myTarget = minetest.env:get_player_by_name(obj:get_player_name())
					if myTarget ~= nil and myTarget:is_player() then
						local light = minetest.env:get_node_light(myTarget:getpos(), nil)
						if minetest.setting_getbool("display_mob_spawn") then
			  				minetest.chat_send_all(myTarget:get_player_name().."(light["..light..">=8])[".. myTarget:getpos().x ..",".. myTarget:getpos().y ..",".. myTarget:getpos().z .."]")
			  			end
						if not light or light > 4 then --Se algum jogador estiver em local claro(light>=8, padrao =13) a morte nao vai nascer la.
							myTarget = nil 
						end 
						if myTarget~=nil and landrush~=nil and landrush.get_owner(myTarget:getpos())~=nil then
							myTarget = nil 
						end
					end
					if myTarget ~= nil and myTarget:is_player() then
						local posTarget = myTarget:getpos()
						if minetest.env:get_node({x=posTarget.x, y=posTarget.y+2, z=posTarget.z}).name ~= "air" then myTarget = nil end -- Se o jogador-alvo estiver dentro da agua ou em uma parede o curse nao persegue.
					end
				elseif obj~=nil and not obj:is_player() then
					--print("[lunomobs:register_spawn('lunomobs:curse')] obj:get_luaentity().name = "..obj:get_luaentity().name)
					--print("[lunomobs:register_spawn('lunomobs:curse')] obj = "..dump(obj))
					--print("[lunomobs:register_spawn('lunomobs:curse')] obj:get_luaentity() = "..dump(obj:get_luaentity()))
					--print("[lunomobs:register_spawn('lunomobs:curse')] obj:get_luaentity().name = "..obj:get_luaentity().name)
					if obj:get_luaentity().name ~= nil and obj:get_luaentity().name == "lunomobs:curse" then
			  			deathCurse = obj:get_luaentity()
			  			if deathCurse ~= nil and minetest.setting_getbool("display_mob_spawn") then
			  				minetest.chat_send_all(deathCurse.name.."[".. deathCurse.object:getpos().x ..",".. deathCurse.object:getpos().y ..",".. deathCurse.object:getpos().z .."]")
			  			end
		  			end
				end
				--[[
				--##########################################################################################################
					FONTE: 
					  * https://github.com/Hackeridze/Ru-true-minetest-game/blob/master/rtmg/mods/mesecons_detector/init.lua
					  * https://github.com/sapier/animals_modpack/blob/master/mobf/mobf.lua
					  * https://forum.minetest.net/viewtopic.php?id=2590 
				--##########################################################################################################
				]]--
			end
	
	
			if myTarget ~= nil then
				local posTarget = myTarget:getpos()
				posTarget.y = posTarget.y + 2
				minetest.sound_play("mobs_death_born", {pos=posTarget, max_hear_distance = RaioDeCurse})
				if minetest.setting_getbool("lunomobs_curseflame") and minetest.registered_items["fire:basic_flame"] ~= nil then
					--############################# FOGO SOBRE A BONE ########################
					minetest.env:set_node({x=pos.x, y=pos.y-1, z=pos.z}, {name="fire:basic_flame"})

					--############################# FOGO SOBRE O JOGADOR #####################
					minetest.env:set_node({x=posTarget.x, y=posTarget.y  , z=posTarget.z}, {name="fire:basic_flame"})
					minetest.env:set_node({x=posTarget.x, y=posTarget.y-1, z=posTarget.z}, {name="fire:basic_flame"})
				end

				if deathCurse == nil then
					deathCurse = minetest.env:add_entity(posTarget, "lunomobs:curse")
				else
					deathCurse.object:setpos(posTarget)
				end

				local frase = math.random(1, 13)
				local mensagem = ""
				if frase == 1 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Retornei mais uma vez para este mundo para comer sua carne, "..myTarget:get_player_name().."!!!" end
				if frase == 2 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! "..myTarget:get_player_name()..", voce nao podem matar quem ja esta morto!" end
				if frase == 3 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Voltei para arrancar a lingua de "..myTarget:get_player_name().." pelo sovaco!!!!" end
				if frase == 4 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! "..myTarget:get_player_name()..", desista e sofra a minha vingaca!" end
				if frase == 5 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Agora e sua vez "..myTarget:get_player_name().."!" end
				if frase == 6 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Voce nao me escapa "..myTarget:get_player_name().."!" end
				if frase == 7 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! A sua hora chegou "..myTarget:get_player_name().."!" end
				if frase == 8 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Vim para darmos um 'pequeno' passeio em Hades, "..myTarget:get_player_name().."!" end
				if frase == 9 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Vim buscar "..myTarget:get_player_name().." para transforma-lo no macaquinho do capeta!" end
				if frase == 10 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Torturarei "..myTarget:get_player_name().." ate a morte, e a morte, e a morte!" end
				if frase == 11 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Vou arrancar as tripas de "..myTarget:get_player_name().." para servir de como meu fio-dental!" end
				if frase == 12 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Voce vai se arrepender por te nascido, "..myTarget:get_player_name().."!" end
				if frase == 13 then mensagem = "<MALDITO CURSE> Ha! Ha! Ha! Ha! Fiz um pacto com o Luciferum, e voltei so para trazer "..myTarget:get_player_name().." comigo para o inferno!!!" end
				--minetest.log('action',"["..minetest.pos_to_string(posTarget).."] "..mensagem)
				minetest.log('action', "["..os.date("%Y-%m-%d %Hh:%Mm:%Ss").."] "..mensagem)
				minetest.chat_send_all(mensagem)
			--[[
			elseif deathCurse ~= nil then
				minetest.sound_play("mobs_death_born", {pos=deathCurse.object:getpos(), max_hear_distance = RaioDeCurse})
				minetest.chat_send_all("<MALDITO CURSE> Ha! Ha! Ha! Ha! Ainda nao foi desta vez que eu levarei mais uma alma para o mundo dos mortos. =S")
				deathCurse.object:remove()
				minetest.env:set_node({x=pos.x, y=pos.y-2, z=pos.z}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-3, z=pos.z}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-3, z=pos.z-1}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-3, z=pos.z+1}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-4, z=pos.z}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-5, z=pos.z}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pos.x, y=pos.y-6, z=pos.z}, {name="fire:basic_flame"})
			]]--
			end 
		end
	end
end)

minetest.register_craftitem("lunomobs:curse", {
	description = "HARRY CURSE",
	groups = { mobs=1 },
	inventory_image = "mobs_curse.png",
	on_place = function(itemstack, placer, pointed_thing)
		if pointed_thing.above then
			--[[
			if minetest.setting_getbool("lunomobs_curseflame") then
				minetest.env:set_node({x=pointed_thing.above.x, y=pointed_thing.above.y  , z=pointed_thing.above.z}, {name="fire:basic_flame"})
				minetest.env:set_node({x=pointed_thing.above.x, y=pointed_thing.above.y-1, z=pointed_thing.above.z}, {name="fire:basic_flame"})
			end
			]]--
			pointed_thing.above.y = pointed_thing.above.y + 1
			minetest.env:add_entity(pointed_thing.above, "lunomobs:curse")
			if not minetest.setting_getbool("creative_mode") then itemstack:take_item() end
		end
		return itemstack
	end,
})

minetest.register_chatcommand("curseflame", {
	params = "<true/false>",
	description = "Configura se o 'Harry CURSE' provova incendio.",
	privs = {},
	func = function(name, param)
		if param=="true" or param=="false" then
			if minetest.get_player_privs(name)["privs"] then
				minetest.setting_set("lunomobs_curseflame",param) 
				minetest.chat_send_player(name, "lunomobs_curseflame = '"..param.."'.")
			else
				minetest.chat_send_player(name, "Voce nao tem o privilegio 'privs' para usar este comando.")
			end
		else
			--minetest.chat_send_player(name, "lunomobs_curseflame='"..minetest.setting_getbool("lunomobs_curseflame").."'")
			minetest.chat_send_player(name, "Sintaxe: /curseflame <true/false>")
		end
	end,
})

minetest.register_alias("curse"	, "lunomobs:curse")
minetest.register_alias("harrycurse"	, "lunomobs:curse")
minetest.register_alias("malditocurse"	, "lunomobs:curse")

--FONTE: https://github.com/PilzAdam/mobs/wiki/API
